package com.lottery.system.controller;

import com.lottery.system.common.errorcode.ControllerErrorCodeConstants;
import com.lottery.system.common.exception.ControllerException;
import com.lottery.system.common.pojo.CommonResult;
import com.lottery.system.common.utils.JacksonUtil;
import com.lottery.system.controller.param.PageParam;
import com.lottery.system.controller.param.addPrizeParam;
import com.lottery.system.controller.result.PrizeListResult;
import com.lottery.system.service.PictureService;
import com.lottery.system.service.PrizeService;
import com.lottery.system.service.dto.PrizeDTO;
import com.lottery.system.service.dto.PageListDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.Serializable;

/**
 * @author: xiaoxie
 * create: 2024-09-06 15:35
 * @BelongsProject: lottery-system
 * @BelongsPackage: com.lottery.system.controller
 * description: 奖品控制层
 */
@RestController
public class PrizeController implements Serializable {
    private static final Logger logger = LoggerFactory.getLogger(PrizeController.class);
    @Autowired
    private PictureService pictureService;
    @Autowired
    private PrizeService prizeService;
    /**
     * @description: 图片上传
     * @author: xiaoxie
     * @date: 2024/9/6 22:28
     * @param: [file]
     * @return: com.lottery.system.common.pojo.CommonResult<java.lang.String>
     **/
    @RequestMapping("/picture/upload")
    public CommonResult<String> pictureUpload(MultipartFile file) {
        logger.info("pictureUpload file:{}", file.getOriginalFilename());
        return CommonResult.success(pictureService.savePicture(file));
    }
    /**
     * @description: 添加单个奖品
     * @author: xiaoxie
     * @date: 2024/9/6 22:30
     * @param: [param, picFile]
     * @return: com.lottery.system.common.pojo.CommonResult<java.lang.Long>
     **/
    @RequestMapping("/prize/add")
    public CommonResult<Long> addPrize(@Validated @RequestPart(value = "param") addPrizeParam param,
                                       @RequestPart(value = "prizePic") MultipartFile picFile) {
        logger.info("addPrize param:{},picFile: {}", param,picFile);
        return CommonResult.success(
                prizeService.addPrize(param,picFile));
    }
    /**
     * @description: 获取奖品列表
     * @author: xiaoxie
     * @date: 2024/9/8 10:10
     * @param: [param]
     * @return: com.lottery.system.common.pojo.CommonResult<com.lottery.system.controller.result.PrizeListResult>
     **/
    @RequestMapping("/get/prizeList")
    public CommonResult<PrizeListResult> getPrizeList(PageParam param) {
        logger.info("getPrizeList : param {}", JacksonUtil.writeValueAsString(param));
        PageListDTO<PrizeDTO> prizeDTOList = prizeService.getPrizeList(param);
        return CommonResult.success(
                converToPrizeListResult(prizeDTOList));
    }

    private PrizeListResult converToPrizeListResult(PageListDTO<PrizeDTO> prizeDTOList) {
        if (null == prizeDTOList ) {
            logger.error(ControllerErrorCodeConstants.PRIZE_LIST_ERROR.getMsg());
            throw new ControllerException(ControllerErrorCodeConstants.PRIZE_LIST_ERROR);
        }
        PrizeListResult prizeListResult = new PrizeListResult();
        prizeListResult.setTotal(prizeDTOList.getTotal());
        prizeListResult.setRecords(prizeDTOList.getPageList().stream().map(
                prizeDTO ->{
                        PrizeListResult.PrizeInfo prizeInfo = new PrizeListResult.PrizeInfo();
                        prizeInfo.setPrizeId(prizeDTO.getPrizeId());
                        prizeInfo.setPrizeName(prizeDTO.getName());
                        prizeInfo.setDescription(prizeDTO.getDescription());
                        prizeInfo.setPrice(prizeDTO.getPrice());
                        prizeInfo.setImageUrl(prizeDTO.getImageUrl());
                        return prizeInfo;
                }
        ).toList());
        return prizeListResult;
    }

}
